package com.estronger.shopmanage.ui.personal;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;
import android.text.InputType;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.estronger.shopmanage.R;
import com.estronger.shopmanage.base.BaseActivity;
import com.estronger.shopmanage.base.MyApplication;
import com.estronger.shopmanage.bean.BeanBase;
import com.estronger.shopmanage.bean.BeanImage;
import com.estronger.shopmanage.manager.NetManager;
import com.estronger.shopmanage.ui.home.LoginActivity;
import com.estronger.shopmanage.ui.order.until.PictureUtil;
import com.estronger.shopmanage.utils.PrefUtils;
import com.estronger.shopmanage.utils.Toast;
import com.estronger.shopmanage.view.RoundImageView;
import com.lidroid.xutils.http.RequestParams;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.soonfor.evaluate.tools.CommonUtils;
import com.soonfor.evaluate.tools.CustomDialog;
import com.soonfor.evaluate.tools.ImageUtils;
import com.soonfor.evaluate.tools.dialog.normal.OnBtnClickL;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

public class PersonalActivity extends BaseActivity implements OnClickListener {
    // 个人中心页
    private RoundImageView ri_photo;
    private AlertDialog chooseDialog;
    private static final int IMAGE_REQUEST_CODE1 = 1, CAMERA_REQUEST_CODE2 = 2,
            REQUESTCODE_CUTTING3 = 3;
    private static String IMAGE_FILE_NAME = PictureUtil.SDPATH + "header.jpg";
    private static final String IMAGE_FILE_LOCATION = PictureUtil.SDPATH + "zhizhi1.jpg";// temp
    Uri imageUri = Uri.parse(IMAGE_FILE_LOCATION);// The Uri to store the big
    private String urlpath, strChange;
    private ProgressDialog pd;
    private TextView user_phone_number;
    private TextView user_QQ_number;
    private TextView user_WeiXin_number;
    private TextView user_Mail_number;
    private TextView user_nike_name;
    private RelativeLayout rl_modifi_psw;
    private RelativeLayout rl_about_us;
    private RelativeLayout rlfExit;

    @Override
    protected void initView() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        QMUIStatusBarHelper.translucent(this);// 沉浸式状态栏
        setContentView(R.layout.activity_personal);
        ri_photo = (RoundImageView) findViewById(R.id.personal_img);
        user_phone_number = (TextView) findViewById(R.id.user_phone_number);
        user_QQ_number = (TextView) findViewById(R.id.user_QQ_number);
        user_QQ_number.setOnClickListener(this);
        user_WeiXin_number = (TextView) findViewById(R.id.user_WeiXin_number);
        user_Mail_number = (TextView) findViewById(R.id.user_Mail_number);
        user_nike_name = (TextView) findViewById(R.id.user_nike_layout);
        rl_modifi_psw = (RelativeLayout) findViewById(R.id.rl_modifi_psw);
        rl_modifi_psw.setOnClickListener(this);
        rl_about_us = (RelativeLayout) findViewById(R.id.rl_about_us);
        rl_about_us.setOnClickListener(this);
        rlfExit = findViewById(R.id.rlfExit);
        rlfExit.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (MyApplication.login != null && MyApplication.login.data != null) {
            MyApplication.imageLoader
                    .displayImage(NetManager.getDownloadURL() + "?filepath="
                            + MyApplication.login.data.headpic, ri_photo);

            user_phone_number.setText(MyApplication.login.data.mobile);
            user_QQ_number.setText(MyApplication.login.data.qq);
            user_WeiXin_number.setText(MyApplication.login.data.linkno);
            user_Mail_number.setText(MyApplication.login.data.email);
            user_nike_name.setText(MyApplication.login.data.usrname);
        }
    }

    public void changePhone(View view) {
        showEdit(R.id.user_phone_number);
    }

    public void changeWeiXin(View view) {
        showEdit(R.id.user_WeiXin_number);
    }

    public void changeMail(View view) {
        showEdit(R.id.user_Mail_number);
    }

    public void changeQQ(View view) {
        showEdit(R.id.user_qq_layout);
    }

    public void changeNike(View view) {
        showEdit(R.id.user_nike_layout);
    }

    /**
     * 编辑对话框
     *
     * @param id
     */
    protected void showEdit(int id) {
        final EditText e = new EditText(this);
        e.setBackgroundResource(R.drawable.red_round_white_kuang);

        View view = LayoutInflater.from(this).inflate(R.layout.view_dialog2,
                null);
        final Dialog dialog = new Dialog(this, R.style.dialog);
        dialog.setContentView(view);
        // 标题
        TextView tView = (TextView) view.findViewById(R.id.tvdialogtitle);
        // 弹窗口的内容
        LinearLayout tcontentLayout = (LinearLayout) view
                .findViewById(R.id.dialogcontent);
        Button okButton = (Button) view.findViewById(R.id.ok);
        Button cancelButton = (Button) view.findViewById(R.id.cancel);
        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                dialog.dismiss();
            }
        });

        switch (id) {
            case R.id.user_phone_number:
                okButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View arg0) {
                        // 判断是否为空
                        strChange = e.getText().toString();
                        if (TextUtils.isEmpty(strChange)) {
                            Toast.show(PersonalActivity.this, "手机号码不能为空", 2000)
                            ;
                            return;
                        }
                        if (!strChange.matches("^[1][1-9][0-9]{9}$")) {
                            Toast.show(PersonalActivity.this, "输入手机号码格式不正确!", 2000)
                            ;
                            return;
                        }

                        // TODO 这里写 修改的网络请求逻辑
                        RequestParams params = new RequestParams();
                        params.addBodyParameter("usercode",
                                MyApplication.getUserCode());
                        params.addBodyParameter("type", "1");
                        params.addBodyParameter("value", strChange);
                        params.addBodyParameter("sf_action", "modifyuser");
                        connect(params, NetManager.CG_PHONE);
                        dialog.dismiss();
                    }
                });
                tView.setText("请输入手机号");
                e.setTextSize(14);
                e.setMaxLines(1);
                e.setPadding(5, 0,5,0);
                e.setLines(1);
                e.setInputType(InputType.TYPE_CLASS_NUMBER);
                e.setFocusable(true);
                // e.setBackgroundResource(drawable.texteditselector);
                tcontentLayout.addView(e);
                dialog.show();
                return;
            case R.id.user_WeiXin_number:
                okButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View arg0) {
                        // 判断是否为空
                        strChange = e.getText().toString();
                        if (TextUtils.isEmpty(strChange)) {
                            Toast.show(PersonalActivity.this, "微信号码不能为空", 2000)
                            ;
                            return;
                        }
                        if (!strChange.matches("^[a-z_\\d]+$")) {
                            Toast.show(PersonalActivity.this, "请正确的输入微信号!", 2000)
                            ;
                            return;
                        }

                        RequestParams params = new RequestParams();
                        params.addBodyParameter("usercode",
                                MyApplication.getUserCode());
                        params.addBodyParameter("type", "3");
                        params.addBodyParameter("value", strChange);
                        params.addBodyParameter("sf_action", "modifyuser");
                        connect(params, NetManager.CG_WEIXIN);
                        dialog.dismiss();
                    }
                });
                tView.setText("请输入微信号");
                e.setTextSize(14);
                e.setLines(1);
                e.setFocusable(true);
                // e.setBackgroundResource(drawable.texteditselector);
                tcontentLayout.addView(e);
                dialog.show();
                return;
            case R.id.user_qq_layout:
                okButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View arg0) {
                        // TODO 这里写 修改的网络请求逻辑
                        strChange = e.getText().toString();
                        // 判断是否为空
                        if (TextUtils.isEmpty(strChange)) {
                            Toast.show(PersonalActivity.this, "QQ号码不能为空", 2000)
                            ;
                            return;
                        }
                        if (!strChange.matches("[1-9][0-9]{4,12}")) {
                            Toast.show(PersonalActivity.this, "请正确的输入QQ号!", 2000)
                            ;
                            return;
                        }

                        RequestParams params = new RequestParams();
                        params.addBodyParameter("usercode",
                                MyApplication.getUserCode());
                        params.addBodyParameter("type", "4");
                        params.addBodyParameter("value", strChange);
                        params.addBodyParameter("sf_action", "modifyuser");
                        connect(params, NetManager.CG_QQ);
                        dialog.dismiss();
                    }
                });
                tView.setText("请输入QQ号");
                e.setTextSize(14);
                e.setLines(1);
                e.setInputType(InputType.TYPE_CLASS_PHONE);
                e.setFocusable(true);
                // e.setBackgroundResource(drawable.texteditselector);
                tcontentLayout.addView(e);
                dialog.show();
                return;
            case R.id.user_Mail_number:
                okButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View arg0) {
                        strChange = e.getText().toString();
                        // 判断是否为空
                        if (TextUtils.isEmpty(strChange.trim())) {
                            Toast.show(PersonalActivity.this, "邮箱不能为空", 2000)
                            ;
                            return;
                        }
                        if (!strChange
                                .matches("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$")) {
                            Toast.show(PersonalActivity.this, "请正确的输入邮箱地址!", 2000)
                            ;
                            return;
                        }
                        // TODO 这里写 修改的网络请求逻辑

                        RequestParams params = new RequestParams();
                        params.addBodyParameter("usercode",
                                MyApplication.getUserCode());
                        params.addBodyParameter("type", "2");
                        params.addBodyParameter("value", strChange);
                        params.addBodyParameter("sf_action", "modifyuser");
                        connect(params, NetManager.CG_MAIL);
                        dialog.dismiss();
                    }
                });
                tView.setText("请输入邮箱");
                e.setTextSize(14);
                e.setFocusable(true);
                e.setLines(1);
                // e.setBackgroundResource(drawable.texteditselector);
                tcontentLayout.addView(e);
                dialog.show();
                return;
            case R.id.user_nike_layout:
                okButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View arg0) {
                        strChange = e.getText().toString();
                        // 判断是否为空
                        if (TextUtils.isEmpty(strChange)) {
                            Toast.show(PersonalActivity.this, "昵称不能为空", 2000)
                            ;
                            return;
                        }
                        if (strChange.length() > 10) {
                            Toast.show(PersonalActivity.this, "昵称限定在1-10个文字", 2000)
                            ;
                            return;
                        }
                        // TODO 这里写 修改的网络请求逻辑

                        RequestParams params = new RequestParams();
                        params.addBodyParameter("usercode",
                                MyApplication.getUserCode());
                        params.addBodyParameter("type", "5");
                        params.addBodyParameter("value", strChange);
                        params.addBodyParameter("sf_action", "modifyuser");
                        connect(params, NetManager.CG_NIKE);
                        dialog.dismiss();
                    }
                });
                tView.setText("请输入昵称");
                e.setTextSize(14);
                e.setFocusable(true);
                e.setMaxLines(1);
                // e.setBackgroundResource(drawable.texteditselector);
                tcontentLayout.addView(e);
                dialog.show();
                return;
            default:
                break;
        }
    }

    public void select_logo(View view) {
        ImageUtils.choosePicture(this, PictureConfig.CHOOSE_REQUEST, 1, new ArrayList<LocalMedia>());
    }

    public void logout(View view) {
        PrefUtils.setBoolean(this, "isLogin", false);
        PrefUtils.setString(this, "loginData", "");
        PrefUtils.setBoolean(this, "isChangeLogo", false);
        PrefUtils.setString(this, "userPwd", "");
        startActivity(new Intent(this, LoginActivity.class));
        MyApplication.finishMain();
        finish();
    }

    @Override
    protected void onActivityResult(int request, int result, Intent data) {
        super.onActivityResult(request, result, data);
        if (result == 666) {
            logout(null);
        }
        switch (request) {
            case IMAGE_REQUEST_CODE1:// 直接从相册获取
                if (data != null) {
                    try {
                        startPhotoZoom(data.getData());
                    } catch (NullPointerException e) {
                        e.printStackTrace();// 用户点击取消操作
                    }
                }
                break;
            case CAMERA_REQUEST_CODE2:// 调用相机拍照
                // 跳转到系统照相机
                Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                File mTmpFile = new File(IMAGE_FILE_NAME);
                if(cameraIntent.resolveActivity(this.getPackageManager()) != null){
                    // 设置系统相机拍照后的输出路径
                    // 创建临时文件

                    if(mTmpFile != null && mTmpFile.exists()) {
                        try {
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                                Uri imageUriFromCamera = FileProvider.getUriForFile(this,
                                        "com.estronger.shopmanage.fileprovider", mTmpFile);
                                startPhotoZoom(imageUriFromCamera);
                            } else {
                                startPhotoZoom(Uri.fromFile(mTmpFile));
                            }
                        }catch (Exception e){
                            android.widget.Toast.makeText(this, "图片错误", android.widget.Toast.LENGTH_SHORT).show();
                        }
                    }else{
                        android.widget.Toast.makeText(this, "图片错误", android.widget.Toast.LENGTH_SHORT).show();
                    }
                }else{
                    android.widget.Toast.makeText(this, "No system camera found", android.widget.Toast.LENGTH_SHORT).show();
                }
                break;
            case REQUESTCODE_CUTTING3:// 取得裁剪后的图片
                if (data != null) {
                    if (imageUri != null) {

                    }
                }
            case PictureConfig.CHOOSE_REQUEST:
                if (data != null) {
                    List<LocalMedia> medias = PictureSelector.obtainMultipleResult(data);
                    if (medias != null && medias.size() > 0) {
                        urlpath = medias.get(0).getCompressPath();
                        try {
                            if (urlpath != null && !urlpath.equals("")) {
                                showDialog("头像上传中...", false);
                                // 开启一个线程 上传头像
                                new Thread(uploadImageRunnable).start();
                            }
                        } catch (Exception e) {}
                    } else {
                        Toast.show(PersonalActivity.this, "未选择任何图片", 2000);
                    }
                }
                break;
        }
    }

    /**
     * 弹出选择图片对话框
     */
    private void showChooseDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this,
                R.style.Theme_Transparent);
        // 填充一个自定义对话框
        View view = LayoutInflater.from(this).inflate(
                R.layout.dialog_choice_pic, null);
        chooseDialog = builder.create();
        chooseDialog.setView(view, 0, 0, 0, 0);
        chooseDialog.show();
        ((RelativeLayout) view.findViewById(R.id.rl_from_album))
                .setOnClickListener(this);
        ((RelativeLayout) view.findViewById(R.id.rl_from_camera))
                .setOnClickListener(this);
        ((RelativeLayout) view.findViewById(R.id.rl_cancel))
                .setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.rl_from_album:// 从相册获取
                chooseDialog.dismiss();
                ImageUtils.takePictureBySysAlbum(PersonalActivity.this, IMAGE_REQUEST_CODE1);
                break;
            case R.id.rl_from_camera:// 从照相机获取
                File file = new File(IMAGE_FILE_NAME);
                chooseDialog.dismiss();
                ImageUtils.takePictureBySysCamera(PersonalActivity.this, file,CAMERA_REQUEST_CODE2);
                break;
            case R.id.rl_cancel:
                chooseDialog.dismiss();
                break;
            case R.id.user_QQ_number:
                showEdit(R.id.user_QQ_number);
                break;
            case R.id.rl_modifi_psw:
                intent = new Intent();
                intent.setClass(this, ChangePasswordActivity.class);
                startActivityForResult(intent, 666);
                break;
            case R.id.rl_about_us:
                intent = new Intent();
                intent.setClass(this, AboutUsActivity.class);
                startActivity(intent);
                break;
            case R.id.rlfExit:
                CustomDialog.getNormalDialog(PersonalActivity.this, "温馨提示", "确定要退出当前账号吗？", new OnBtnClickL() {
                    @Override
                    public void onBtnClick(View view) {
                        logout(null);
                    }
                });
                break;
            default:
                break;
        }
    }

    private String currPath;

    @Override
    protected void onNetSuccess(int code, String result) {
        super.onNetSuccess(code, result);
        if (code == NetManager.IMGUPDATE) {
            BeanImage bean = gson.fromJson(result, BeanImage.class);
            if (bean.error == 0) {
                currPath = bean.filepath;
                if (!currPath.equals("") && currPath != null) {
                    RequestParams params = new RequestParams();
                    params.addBodyParameter("usercode", MyApplication.getUserCode());
                    params.addBodyParameter("headurl", bean.filepath);
                    params.addBodyParameter("sf_action", "uploadheadpic");
                    params.addBodyParameter("regid", CommonUtils.getIMEI(PersonalActivity.this));
                    connect(params, NetManager.UP_AVATAR);
                } else {
                    Toast.show(this, "头像上传失败", 2000);
                    dimssDialog();
                }
            }
        }
        if (code == NetManager.UP_AVATAR) {
            BeanBase bean = gson.fromJson(result, BeanBase.class);
            if (bean.success) {
                Toast.show(this, "恭喜您，新头像上传成功", 2000);
                MyApplication.imageLoader.displayImage(
                        NetManager.getDownloadURL() + "?filepath=" + currPath,
                        ri_photo);
//                PrefUtils.setBoolean(this, "isChangeLogo", true);
//                PrefUtils.setString(this, "changeLogoPath",
//                        NetManager.getDownloadURL() + "?filepath=" + currPath);

                //更新login公共类的头像地址
                MyApplication.login.data.headpic = currPath;


//                Toast.show(this, "恭喜您，新头像上传成功", 2000);
//                MyApplication.imageLoader.displayImage(
//                        NetManager.getDownloadURL() + "?filepath=" + currPath,
//                        ri_photo);
//                // BitmapHelper.getBitmapUtils().display(photo,
//                // NetManager.imgDownloadURL2 + "?filepath=" + currPath);
//                PrefUtils.setBoolean(this, "isChangeLogo", true);
//                PrefUtils.setString(this, "changeLogoPath",
//                        NetManager.getDownloadURL() + "?filepath=" + currPath);
            } else {
                Toast.show(this, "头像上传失败:" + bean.msg, 2000);
            }
            dimssDialog();
        }
        if (code == NetManager.CG_QQ) {
            BeanBase bean = gson.fromJson(result, BeanBase.class);
            if (bean.success) {
                user_QQ_number.setText(strChange);
                MyApplication.login.data.qq = strChange;
                Toast.show(this, "恭喜您，QQ号码已更新", 2000);
                PrefUtils.setBoolean(this, "isLogin", false);
                PrefUtils.setString(this, "loginData", "");
            } else {
                Toast.show(this, "修改QQ号码失败", 2000);
            }
        }
        if (code == NetManager.CG_MAIL) {
            BeanBase bean = gson.fromJson(result, BeanBase.class);
            if (bean.success) {
                user_Mail_number.setText(strChange);
                MyApplication.login.data.email = strChange;
                Toast.show(this, "已成功更改邮箱号码", 2000);
                PrefUtils.setBoolean(this, "isLogin", false);
                PrefUtils.setString(this, "loginData", "");
            } else {
                Toast.show(this, "修改邮箱号码失败", 2000);
            }
        }
        if (code == NetManager.CG_NIKE) {
            BeanBase bean = gson.fromJson(result, BeanBase.class);
            if (bean.success) {
                user_nike_name.setText(strChange);
                MyApplication.login.data.usrname = strChange;
                Toast.show(this, "已成功更改昵称", 2000);
                PrefUtils.setBoolean(this, "isLogin", false);
                PrefUtils.setString(this, "loginData", "");
            } else {
                Toast.show(this, "修改昵称失败", 2000);
            }
        }
        if (code == NetManager.CG_PHONE) {
            BeanBase bean = gson.fromJson(result, BeanBase.class);
            if (bean.success) {
                user_phone_number.setText(strChange);
                MyApplication.login.data.mobile = strChange;
                Toast.show(this, "修改手机号码成功", 2000);
                PrefUtils.setBoolean(this, "isLogin", false);
                PrefUtils.setString(this, "loginData", "");
            } else {
                Toast.show(this, "修改手机号码失败", 2000);
            }
        }
        if (code == NetManager.CG_WEIXIN) {
            BeanBase bean = gson.fromJson(result, BeanBase.class);
            if (bean.success) {
                user_WeiXin_number.setText(strChange);
                MyApplication.login.data.linkno = strChange;
                Toast.show(this, "修改微信号码成功", 2000);
                PrefUtils.setBoolean(this, "isLogin", false);
                PrefUtils.setString(this, "loginData", "");
            } else {
                Toast.show(this, "修改微信号码失败", 2000);
            }
        }

        if (code == NetManager.LOGOUT) {
            Toast.show(this, "退出账号成功", 2000);
            PrefUtils.setBoolean(this, "isLogin", false);
            PrefUtils.setString(this, "loginData", "");
            PrefUtils.setBoolean(this, "isChangeLogo", false);
            PrefUtils.setString(this, "userPwd", "");
            startActivity(new Intent(this, LoginActivity.class));
            MyApplication.finishMain();
            finish();
        }
    }

    @Override
    protected void onNetErorr(int code, String msg) {
        super.onNetErorr(code, msg);
        if (pd != null && pd.isShowing()) {
            pd.dismiss();
        }
    }

    /**
     * 裁剪图片方法实现
     *
     * @param uri uri地址
     */
    public void startPhotoZoom(Uri uri) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        // crop=true是设置在开启的Intent中设置显示的VIEW可裁剪
        intent.putExtra("crop", "true");
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        intent.putExtra("outputX", 200);
        intent.putExtra("outputY", 200);
        intent.putExtra("scale", true);
        intent.putExtra("return-data", false);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
        intent.putExtra("noFaceDetection", true); // no face detection
        startActivityForResult(intent, REQUESTCODE_CUTTING3);
    }

    private Bitmap decodeUriAsBitmap(Uri uri) {
        Bitmap bitmap = null;
        try {
            bitmap = BitmapFactory.decodeStream(this.getContentResolver()
                    .openInputStream(uri));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        }
        return bitmap;
    }

    /**
     * 保存裁剪之后的图片数据 并且设置到控件上面去
     *
     */
    private void setPicToView1(String compressPath) {
        if (compressPath != null && !compressPath.equals("")) {
            urlpath = compressPath;
            showDialog("正在上传头像，请稍候...", false);
            // 开启一个线程 上传头像
            new Thread(uploadImageRunnable).start();
        }
    }

    private void setPicToView(String urlpath) {
        this.urlpath = urlpath;
        // pd = ProgressDialog.show(this, null, "正在上传头像，请稍候...");
        showDialog("正在上传头像，请稍候...", false);
        // 开启一个线程 上传头像
        new Thread(uploadImageRunnable).start();
    }

    Runnable uploadImageRunnable = new Runnable() {
        @Override
        public void run() {
            // TODO 这里写上传逻辑
            if(urlpath!=null && !urlpath.equals("")) {
                File file = new File(urlpath);
                RequestParams params = new RequestParams();
                params.addBodyParameter("usercode", MyApplication.getUserCode());
                params.addBodyParameter("dir", "image");
                params.addBodyParameter("imgFile", file);
                connect(params, NetManager.IMGUPDATE);
            }
        }
    };
}